<?php
/**
 * @Author:          zk
 * @DateTime:        18-5-10 下午4:05
 * @Description:     收货地址
 */
class PMSReceivesAddress extends Eloquent
{
    protected $table = "pms_receives_address";

    /**
     * @Author:          zk
     * @DateTime:        18-5-10 下午4:30
     * @Description:     检验门店默认收货人信息   收货人信息 [该门店 收货人信息不存在时 ，使用门店信息 为店长id排序]
     * @param int $shop_id      门店id
     * @param int $admin_id     管理员id
     * @return boolean  true:门店存在默认收货地址信息;false:门店默认收货地址初始化失败;
     */
    public static function checkShopDefaultAddress($shop_id, $admin_id){
        $info = PMSReceivesAddress::where('shop_id', $shop_id)->where('status', 1)->first();
        if(is_null($info)){
            $sc_user_info = MaintenanceShops::leftJoin('sc_user', 'maintenance_shops.id', '=', 'sc_user.shop_id')
                    ->where('maintenance_shops.id', $shop_id)->where('maintenance_shops.status', 1)
                    ->where('sc_user.title', 1)->where('sc_user.status', 1)
                    ->select('maintenance_shops.address', 'sc_user.real_name', 'sc_user.phone')
                    ->orderBy('sc_user.id', 'ASC')->first();
            $res = new PMSReceivesAddress();
            $res->shop_id = $shop_id;
            $res->name = $sc_user_info->real_name;
            $res->phone = $sc_user_info->phone;
            $res->address = $sc_user_info->address;
            $res->status = 1;
            $res->created_at = date('Y-m-d H:i:s', time());
            $res->created_by = $admin_id;
            $res = $res->save();
        } else {
            $res = TRUE;
        }
        
        return $res;
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-5-14 上午11:50
     * @Description:     根据门店id获取该门店的默认收货地址
     * @param int $shop_id      门店id
     * @return object
     */
    public static function getShopDefaultAddress($shop_id){
        return PMSReceivesAddress::where('shop_id', $shop_id)->where('status', 1)->first();
    }
    
}